<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME1;
  $password = PASSWORD1;
  $dbname = DATABASE_NAME1;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {
  echo $e->getMessage ();
}

try {
  $sqls = array();
  
  $sql = "select e.e_oid as id, e.createdDate, e.payDate, e.cancelledDate from ns_expense as e";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0; $results = array();
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->createdDate = ($tmp->createdDate != 0) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
	$tmp->payDate = ($tmp->payDate != 0) ? SimpleDate::fromStamp($tmp->payDate) : NULL;
	$tmp->cancelledDate = ($tmp->cancelledDate != 0) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
	$results[] = $tmp;
    $i++;
  }
  
  foreach ($results as &$r) {
    $sql = 'update ns_expense as e set e.createdDate=' . (($r->createdDate) ? SimpleDate::toStamp(SimpleDate::create($r->createdDate->year, $r->createdDate->month, $r->createdDate->date)) : 0) . ', ' . 'e.payDate=' . (($r->payDate) ? SimpleDate::toStamp(SimpleDate::create($r->payDate->year, $r->payDate->month, $r->payDate->date)) : 0) . ', ' . 'e.cancelledDate=' . (($r->cancelledDate) ? SimpleDate::toStamp(SimpleDate::create($r->cancelledDate->year, $r->cancelledDate->month, $r->cancelledDate->date)) : 0) . ' where e.e_oid = ' . $r->id;
	$sqls[] = $sql;
  }
  
  foreach ($sqls as &$sq) {
    MiscUtils::dump($sq);
    $myPdo->exec($sq);
  }
  
} catch (PDOException $e) {
  echo $e->getMessage();
}
?>